package com.zhangtai.eSign.pdf;


import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.*;
import com.zhangtai.modules.constVo.ContractConst;
import com.zhangtai.modules.controller.admin.JobController;
//import com.zhangtai.modules.dao.ssqlDao.SsqlSalaryGoupDao;
import com.zhangtai.modules.dto.*;
import com.zhangtai.modules.entity.certificate.CertificateEntity;
import com.zhangtai.modules.entity.contract.ContractMangerEntity;
import com.zhangtai.modules.entity.contract.ContractModelEntity;
import com.zhangtai.modules.entity.contract.ProtocolEntity;
import com.zhangtai.modules.entity.contract.ProtocolPersonnelEntity;
import com.zhangtai.modules.entity.employee.DimissionSettlementEntity;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.employee.EmployerCompanyEntity;
import com.zhangtai.modules.entity.employee.RelativeRecordsEntity;
import com.zhangtai.modules.service.contract.ContractMangerService;
import com.zhangtai.modules.service.dimission.DimissionSettlementService;
import com.zhangtai.modules.service.employee.EmployeeInfoService;
import com.zhangtai.modules.vo.ContractTerminatePdfVo;
import com.zhangtai.modules.vo.ExportContractPdfVo;
import com.zhangtai.utils.BeanUtilsMabach;
import com.zhangtai.utils.DateUtils;
import com.zhangtai.utils.R;
import com.zhangtai.utils.StringUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ming
 * @Date: 2020/5/26 0026 下午 3:46
 */
@Slf4j
@Component
public class ContractPdf {


//    @Autowired
//    private SsqlSalaryGoupDao ssqlSalaryGoupDao;

    @Autowired
    private EmployeeInfoService employeeInfoService;
    @Autowired
    private JobController jobController;

    @Autowired
    private ContractMangerService contractMangerService;

    @Autowired
    private DimissionSettlementService dimissionSettlementService;


    @Value("${pdf.path}")
    String path;


    /**
     * 固定模板的劳动合同pdf
     * @param vo
     * @param companyEntity
     * @return
     */
    public R createPdfNew(ExportContractPdfVo vo, EmployerCompanyEntity companyEntity, EmployerCompanyEntity serverCompany,
                          ContractModelEntity model, java.util.List<RelativeRecordsEntity>relativeRecordsEntityList,
                          JSONObject userInfo){
        Long oid = Long.parseLong(vo.getOid());
        EmployeeInfoEntity entity = employeeInfoService.getByIdE(oid);
//        ssqlSalaryGoupDao.getSalaryGroupById(entity.getSalaryGroupId());
        SalaryGroupDto salaryGroupDto = new SalaryGroupDto();
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        String newPath = path+model.getName()+oid+".pdf";
        try {
            Font  FontChinese = getPdfChineseFont();
            BaseFont  bf = FontChinese.getBaseFont();
            out = new FileOutputStream(newPath);// 输出流
            reader = new PdfReader(model.getContractPath());// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            form.addSubstitutionFont(bf);
            Object dto = null;
            Field[] fields = null;
            //实习协议
            if(ContractConst.CONTRACT_MANGER_INTERNSHIP.equals(vo.getContractType())){
                dto = handleInternshipContract(entity,companyEntity,vo);
                fields = ContractInternshipPdfDto.class.getDeclaredFields();
            }
            //返聘协议
            else if(ContractConst.CONTRACT_MANGER_REEMPLOYEE.equals(vo.getContractType())){
                dto = handleReEmployeeContract(entity,companyEntity,vo,salaryGroupDto);
                fields = ContractReEmployeePdfDto.class.getDeclaredFields();
            }
            //解除协议
            else if(ContractConst.CONTRACT_MANAGER_TERMINATE.equals(vo.getContractType())){
                dto = handleTerminate(entity,companyEntity,vo,userInfo);
                fields = ContractTerminatePdfVo.class.getDeclaredFields();
            }
            //劳动合同
            else {
                dto = handleNewContract(entity,companyEntity,vo,salaryGroupDto,serverCompany);
                fields = ContractPdfDto.class.getDeclaredFields();
            }

            Map<String,String> apiName = new HashMap<>();
            for(Field field: fields){
                //设置些属性是可以访问的
                field.setAccessible(true);
                //获取属性的类型
                boolean isAnn = field.isAnnotationPresent(ApiModelProperty.class);
                if (!isAnn) {
                    continue;
                }
                ApiModelProperty ann = null;
                try {
                    ann = field.getAnnotation(ApiModelProperty.class);
                } catch (Exception e) {
                    continue;
                }
                if(StringUtils.isEmpty(ann.value())) {
                    continue;
                }else{
                    apiName.put(field.getName(),ann.value());
                }
            }

            Map map =  BeanMap.create(dto);
            boolean isEmpty = false;
            String emptyName ="" ;
            for(Object key : map.keySet()){
                String value = (String)map.get(key);
                if(StringUtils.isEmpty(value)&&!StringUtils.isEmpty(apiName.get(key.toString()))){
                    emptyName = apiName.get(key.toString());
                    isEmpty = true;
                    break;
                }
                if(key.toString().indexOf("Large")>=0){
                    if(key.toString().indexOf("property")>=0){
                        form.setFieldProperty(key.toString(),"textsize",10f,null);
                    }else{
                        form.setFieldProperty(key.toString(),"textsize",15f,null);
                    }
                }else if("jobnamne".equals(key.toString())){
                    form.setFieldProperty(key.toString(),"textsize",10f,null);
                }else{
                    form.setFieldProperty(key.toString(),"textsize",11f,null);
                }

                form.setField(key.toString(),(String)map.get(key));
            }
            int j=1;
            for(RelativeRecordsEntity entity1:relativeRecordsEntityList){
                form.setField("jobname"+j,entity1.getJob());
                form.setField("relativeName"+j,entity1.getName());
                form.setField("unitname"+j,entity1.getUnit());
                form.setField("relative"+j,entity1.getRelative());
                j++;
            }
            if(isEmpty){
                stamper.close();
                reader.close();
                return R.error(emptyName+"信息为空");
            }
            stamper.setFormFlattening(true);// 如果为false，生成的PDF文件可以编辑，如果为true，生成的PDF文件不可以编辑
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            for(int i=1;i<model.getContractNumber()+1;i++){
                PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
                copy.addPage(importPage);

            }
            doc.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok(newPath);
    }

    /**
     * 固定模板的培训协议pdf
     * @param
     * @param companyEntity
     * @return
     */
    public R createProtocolPdf(ProtocolEntity protocolEntity, ProtocolPersonnelEntity protocolPersonnelEntity, EmployerCompanyEntity companyEntity,ContractModelEntity model){
        Long oid = protocolPersonnelEntity.getOid();
        EmployeeInfoEntity employeeInfoEntity = employeeInfoService.getByIdE(oid);
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        String newPath = path+model.getName()+oid+".pdf";
        try {
            Font  FontChinese = getPdfChineseFont();
            BaseFont  bf = FontChinese.getBaseFont();
            out = new FileOutputStream(newPath);// 输出流
            reader = new PdfReader(model.getContractPath());// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            form.addSubstitutionFont(bf);
            ProtocolPdfDto dto = new ProtocolPdfDto();

            dto.setCompany(companyEntity.getName());
            dto.setRepresentative(companyEntity.getRepresentative());
            dto.setName(employeeInfoEntity.getName());
            dto.setIdCard(employeeInfoEntity.getIdcard());
            dto.setGender(employeeInfoEntity.getGender().equals("1")?"男":"女");
            dto.setUnitname(jobController.orgMap.get(employeeInfoEntity.getUnitId()));
            dto.setJobname(jobController.jobMap.get(employeeInfoEntity.getJobId()));
            dto.setProjectName(protocolEntity.getProjectName());
            dto.setTrainingStructure(protocolEntity.getTrainingStructure());

            String trainingStartTime=DateUtils.format(protocolEntity.getTrainingStartTime());
            dto.setTrainingStartTimeYear(trainingStartTime.substring(0,4));
            dto.setTrainingStartTimMonth(trainingStartTime.substring(5,7));
            dto.setTrainingStartTimeDay(trainingStartTime.substring(8,10));

            String trainingEndTime=DateUtils.format(protocolEntity.getTrainingEndTime());
            dto.setTrainingEndTimeYear(trainingEndTime.substring(0,4));
            dto.setTrainingEndTimeMonth(trainingEndTime.substring(5,7));
            dto.setTrainingEndTimeDay(trainingEndTime.substring(8,10));

            String method="A";
            if(0==protocolEntity.getTrainingMethod()){
                method="A";
            }
            if(1==protocolEntity.getTrainingMethod()){
                method="B";
            }
            if(2==protocolEntity.getTrainingMethod()){
                method="C";
            }
            dto.setTrainingMethod(method);

            String[] strs= protocolEntity.getTrainingLocation().split(",");
            String trainingLocation="";
            for(String str:strs){
                trainingLocation+=jobController.getPaycity(Long.valueOf(str));
            }
            dto.setTrainingLocation(trainingLocation);

            String trainingSpending=protocolPersonnelEntity.getTrainingSpending() ==null?"0":String.format("%.0f",protocolPersonnelEntity.getTrainingSpending());
            dto.setTrainingSpending(trainingSpending);
            String trainingFoodSpending=protocolPersonnelEntity.getTrainingFoodSpending() ==null?"0":String.format("%.0f",protocolPersonnelEntity.getTrainingFoodSpending());
            dto.setTrainingFoodSpending(trainingFoodSpending);
            String trainingTotal=protocolPersonnelEntity.getTrainingTotal() ==null?"0":String.format("%.0f",protocolPersonnelEntity.getTrainingTotal());
            dto.setTrainingTotalCapital(Convert.digitToChinese(Double.valueOf(trainingTotal)));
            dto.setTrainingTotal(trainingTotal);

            String serviceStartTime=DateUtils.format(protocolPersonnelEntity.getServiceStartTime());
            dto.setServiceStartTimeYear(serviceStartTime.substring(0,4));
            dto.setServiceStartTimeMonth(serviceStartTime.substring(5,7));
            dto.setServiceStartTimeDay(serviceStartTime.substring(8,10));

            String serviceEndTime=DateUtils.format(protocolPersonnelEntity.getServiceEndTime());
            dto.setServiceEndTimeYear(serviceEndTime.substring(0,4));
            dto.setServiceEndTimeMonth(serviceEndTime.substring(5,7));
            dto.setServiceEndTimeDay(serviceEndTime.substring(8,10));

            String signedTime=DateUtils.format(protocolEntity.getSignedTime());
            dto.setSignedTimeYear(signedTime.substring(0,4));
            dto.setSignedTimeMonth(signedTime.substring(5,7));
            dto.setSignedTimeDay(signedTime.substring(8,10));

            Map map =  BeanMap.create(dto);
            for(Object key : map.keySet()){
                if(key.toString().indexOf("Large")>=0){
                    if(key.toString().indexOf("property")>=0){
                        form.setFieldProperty(key.toString(),"textsize",10f,null);
                    }else{
                        form.setFieldProperty(key.toString(),"textsize",15f,null);
                    }
                }else if("jobnamne".equals(key.toString())){
                    form.setFieldProperty(key.toString(),"textsize",10f,null);
                }else{
                    form.setFieldProperty(key.toString(),"textsize",11f,null);
                }

                form.setField(key.toString(),(String)map.get(key));
            }
            stamper.setFormFlattening(true);// 如果为false，生成的PDF文件可以编辑，如果为true，生成的PDF文件不可以编辑
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            for(int i=1;i<model.getContractNumber()+1;i++){
                PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
                copy.addPage(importPage);
            }
            doc.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(newPath);
    }

    public R createCertificatePdf(EmployerCompanyEntity companyEntity, ContractModelEntity model, CertificateEntity entity,EmployeeInfoEntity infoEntity){
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        String newPath = path+model.getName()+infoEntity.getOid()+".pdf";
        Date date = new Date();
        try {
            Font  FontChinese = getPdfChineseFont();
            BaseFont  bf = FontChinese.getBaseFont();
            out = new FileOutputStream(newPath);// 输出流
            reader = new PdfReader(model.getContractPath());// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            form.addSubstitutionFont(bf);
            CertificatePdfDto dto = BeanUtilsMabach.doToDto(entity,CertificatePdfDto.class);
            dto.setDay(DateUtils.format(date,"dd"));
            dto.setMonth(DateUtils.format(date,"MM"));
            dto.setYear(DateUtils.format(date,"yyyy"));
            dto.setUnitName(jobController.orgMap.get(entity.getUnitId())+"-"+jobController.jobMap.get(entity.getJobId() ));
            dto.setEmployeeStatus(StringUtils.isEmpty(infoEntity.getEmployeestatus())?"":
                    "2".equals(infoEntity.getEmployeestatus())?"正式":
                    "9".equals(infoEntity.getEmployeestatus())?"实习":"离职");
            dto.setIdcard(infoEntity.getIdcard());
            dto.setMarriage(StringUtils.isEmpty(infoEntity.getMarital())?"":"1".equals(infoEntity.getMarital())?"未婚":"已婚");
            dto.setSalaryCapital(Convert.digitToChinese(dto.getSalary()));
            dto.setWorkAge(DateUtils.yearsBetween(infoEntity.getHiredate(),date).toString());
            Map map =  BeanMap.create(dto);
            for(Object key : map.keySet()){
               String value = "";
                if(map.get(key) instanceof BigDecimal){
                    BigDecimal o = (BigDecimal) map.get(key);
                    value =  o.toString();
                }else{
                    value = ObjectUtils.isEmpty(map.get(key))?"":map.get(key).toString();
                }
                form.setFieldProperty(key.toString(),"textsize",14f,null);
                form.setField(key.toString(),value);
            }
            stamper.setFormFlattening(true);// 如果为false，生成的PDF文件可以编辑，如果为true，生成的PDF文件不可以编辑
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            for(int i=1;i<model.getContractNumber()+1;i++){
                PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
                copy.addPage(importPage);
            }
            doc.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(newPath);
    }

    private  Font getPdfChineseFont() throws Exception {
//        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
//                BaseFont.NOT_EMBEDDED);
        BaseFont bfChinese = BaseFont.createFont(path+"fangsong.ttf", BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED);
        Font fontChinese = new Font(bfChinese, 3, Font.NORMAL);
        return fontChinese;
    }

    /**
     * 新签劳动合同
     * @param entity
     * @param companyEntity
     * @param vo
     * @param salaryGroupDto
     * @param serverCompany
     * @return
     */
    private Object handleNewContract(EmployeeInfoEntity entity,EmployerCompanyEntity companyEntity,
                                     ExportContractPdfVo vo,SalaryGroupDto salaryGroupDto,EmployerCompanyEntity serverCompany){
        ContractPdfDto dto = BeanUtilsMabach.doToDto(entity,ContractPdfDto.class);
        if(!ObjectUtils.isEmpty(serverCompany)){
            dto.setServerCompanyName(serverCompany.getName());
        }
        dto.setServerName(vo.getServerName());
        dto.setAddressLarge(entity.getAddress());
        dto.setMobileLarge(entity.getMobile());
        dto.setAgeLarge(DateUtils.yearsBetween(entity.getBirthday(),new Date()).toString());
        dto.setNativeplace(entity.getRegresidence());
        dto.setNativeplaceLarge(entity.getRegresidence());
        dto.setRepresentativeLimit(vo.getRepresentativeLimit());
        dto.setCompany(companyEntity.getName());
        dto.setCompanyLarge(companyEntity.getName());
        dto.setProperty(companyEntity.getProperty());
        dto.setPropertyLarge(companyEntity.getProperty());
        if("0".equals(dto.getRepresentativeLimit())){
            dto.setRepresentativeLimit("0.5");
        }
        dto.setRepresentative(companyEntity.getRepresentative());
        dto.setRepresentativeLarge(companyEntity.getRepresentative());
        dto.setRegisteredAddress(companyEntity.getRegisteredAddress());
        dto.setRegisteredAddressLarge(companyEntity.getRegisteredAddress());
        dto.setIdcardLarge(entity.getIdcard());
        dto.setGender(entity.getGender().equals("1")?"男":"女");
        dto.setGenderLarge(entity.getGender().equals("1")?"男":"女");
        dto.setUnitname(jobController.orgMap.get(entity.getUnitId()));
        dto.setJobname(jobController.jobMap.get(entity.getJobId()));
        dto.setOrgname(jobController.orgMap.get(entity.getOrgId()));
        dto.setNameLarge(entity.getName());
        if(!ObjectUtils.isEmpty(vo.getSalaryDate())){
            dto.setSalaryDay(vo.getSalaryDate());
        }else if(ObjectUtils.isEmpty(salaryGroupDto)||StringUtils.isEmpty(salaryGroupDto.getCpaydate())){
            dto.setSalaryDay("15");
        }else{
            dto.setSalaryDay(salaryGroupDto.getCpaydate());
        }
        if(!ObjectUtils.isEmpty(entity.getProbationday()))
        dto.setProbationday(entity.getProbationday().toString());
        dto.setHiredateYear(vo.getRepresentativeStart().substring(0,4));
        dto.setHiredateMonth(vo.getRepresentativeStart().substring(5,7));
        dto.setHiredateDay(vo.getRepresentativeStart().substring(8,10));

        dto.setHiredateEndYear(vo.getRepresentativeEnd().substring(0,4));
        dto.setHiredateEndMonth(vo.getRepresentativeEnd().substring(5,7));
        dto.setHiredateEndDay(vo.getRepresentativeEnd().substring(8,10));

        //如果是调动签(自动带入试用时间)
        if(ContractConst.CONTRACT_TYPE_TRANSFER.equals(vo.getType())){
            dto.setProbationBeginYear(dto.getHiredateYear());
            dto.setProbationBeginMonth(dto.getHiredateMonth());
            dto.setProbationBeginDay(dto.getHiredateDay());
            if(!ObjectUtils.isEmpty(entity.getProbationday())){
                Date regularDay = DateUtils.addDateMonths(entity.getHiredate(),entity.getProbationday());
                dto.setProbationEndYear(DateUtils.format(regularDay,"yyyy"));
                dto.setProbationEndMonth(DateUtils.format(regularDay,"MM"));
                dto.setProbationEndDay(DateUtils.format(regularDay,"dd"));
                int days = DateUtils.daysBetween(new Date(),regularDay);
                String probationday = new BigDecimal(days).divide(new BigDecimal(30),1, BigDecimal.ROUND_HALF_UP).toString();
                dto.setProbationday(probationday);
            }
        }else{
            if(!ObjectUtils.isEmpty(entity.getProbationday())){
                Date endDate = DateUtils.addDateMonths(entity.getHiredate(),entity.getProbationday());
                endDate = DateUtils.addDateDays(endDate,-1);
                dto.setProbationEndYear(DateUtils.format(endDate,"yyyy"));
                dto.setProbationEndMonth(DateUtils.format(endDate,"MM"));
                dto.setProbationEndDay(DateUtils.format(endDate,"dd"));
            }
            dto.setProbationBeginYear(DateUtils.format(entity.getHiredate(),"yyyy"));
            dto.setProbationBeginMonth(DateUtils.format(entity.getHiredate(),"MM"));
            dto.setProbationBeginDay(DateUtils.format(entity.getHiredate(),"dd"));
        }

        dto.setServer(vo.getServer());
        dto.setWorkAddress(vo.getWorkAddress());
        dto.setSalary(vo.getSalary());
        dto.setContractAddress(vo.getContractAddress());
        dto.setDate(vo.getRepresentativeStart());
        dto.setRepresentativeStartYear(vo.getRepresentativeStart().substring(0,4));
        dto.setRepresentativeStartMonth(vo.getRepresentativeStart().substring(5,7));
        dto.setRepresentativeStartDay(vo.getRepresentativeStart().substring(8,10));
        dto.setRepresentativeEndYear(vo.getRepresentativeEnd().substring(0,4));
        dto.setRepresentativeEndMonth(vo.getRepresentativeEnd().substring(5,7));
        dto.setRepresentativeEndDay(vo.getRepresentativeEnd().substring(8,10));
        if(!ObjectUtils.isEmpty(serverCompany)){
            dto.setServerRepresentative(serverCompany.getRepresentative());
        }
        dto.setYear(vo.getRepresentativeStart().substring(0,4));
        dto.setMonth(vo.getRepresentativeStart().substring(5,7));
        dto.setDay(vo.getRepresentativeStart().substring(8,10));
        return dto;
    }

    /**
     * 新签实习协议
     * @param entity
     * @param companyEntity
     * @param vo
     * @return
     */
    private Object handleInternshipContract(EmployeeInfoEntity entity,EmployerCompanyEntity companyEntity,
                                     ExportContractPdfVo vo){
        ContractInternshipPdfDto dto = BeanUtilsMabach.doToDto(entity,ContractInternshipPdfDto.class);
        dto.setCompany(companyEntity.getName());
        dto.setRepresentative(companyEntity.getRepresentative());
        dto.setHiredateYear(vo.getRepresentativeStart().substring(0,4));
        dto.setHiredateMonth(vo.getRepresentativeStart().substring(5,7));
        dto.setHiredateDay(vo.getRepresentativeStart().substring(8,10));
        dto.setHiredateEndYear(vo.getRepresentativeEnd().substring(0,4));
        dto.setHiredateEndMonth(vo.getRepresentativeEnd().substring(5,7));
        dto.setHiredateEndDay(vo.getRepresentativeEnd().substring(8,10));
        dto.setDate(vo.getRepresentativeStart());
        dto.setYear(vo.getRepresentativeStart().substring(0,4));
        dto.setMonth(vo.getRepresentativeStart().substring(5,7));
        dto.setDay(vo.getRepresentativeStart().substring(8,10));
        return dto;
    }

    /**
     * 新签返聘协议
     * @param entity
     * @param companyEntity
     * @param vo
     * @param salaryGroupDto
     * @return
     */
    private Object handleReEmployeeContract(EmployeeInfoEntity entity,EmployerCompanyEntity companyEntity,
                                     ExportContractPdfVo vo,SalaryGroupDto salaryGroupDto){
        ContractReEmployeePdfDto dto = BeanUtilsMabach.doToDto(entity,ContractReEmployeePdfDto.class);

        dto.setAddressLarge(entity.getAddress());
        dto.setMobileLarge(entity.getMobile());
        dto.setAgeLarge(DateUtils.yearsBetween(entity.getBirthday(),new Date()).toString());
        dto.setNativeplace(entity.getRegresidence());
        dto.setNativeplaceLarge(entity.getRegresidence());
        dto.setCompany(companyEntity.getName());
        dto.setCompanyLarge(companyEntity.getName());
        dto.setProperty(companyEntity.getProperty());
        dto.setPropertyLarge(companyEntity.getProperty());
        dto.setRepresentative(companyEntity.getRepresentative());
        dto.setRepresentativeLarge(companyEntity.getRepresentative());
        dto.setRegisteredAddressLarge(companyEntity.getRegisteredAddress());
        dto.setIdcardLarge(entity.getIdcard());
        dto.setGenderLarge(entity.getGender().equals("1")?"男":"女");
        dto.setJobname(jobController.jobMap.get(entity.getJobId()));
        dto.setNameLarge(entity.getName());
        if(!ObjectUtils.isEmpty(vo.getSalaryDate())){
            dto.setSalaryDay(vo.getSalaryDate());
        }else if(ObjectUtils.isEmpty(salaryGroupDto)||StringUtils.isEmpty(salaryGroupDto.getCpaydate())){
            dto.setSalaryDay("15");
        }else{
            dto.setSalaryDay(salaryGroupDto.getCpaydate());
        }
        if(!ObjectUtils.isEmpty(entity.getProbationday())){
            dto.setProbationday(entity.getProbationday().toString());
        }
        dto.setHiredateYear(vo.getRepresentativeStart().substring(0,4));
        dto.setHiredateMonth(vo.getRepresentativeStart().substring(5,7));
        dto.setHiredateDay(vo.getRepresentativeStart().substring(8,10));
        dto.setHiredateEndYear(vo.getRepresentativeEnd().substring(0,4));
        dto.setHiredateEndMonth(vo.getRepresentativeEnd().substring(5,7));
        dto.setHiredateEndDay(vo.getRepresentativeEnd().substring(8,10));
        dto.setWorkAddress(vo.getWorkAddress());
        dto.setSalary(vo.getSalary());
        dto.setContractAddress(vo.getContractAddress());
        dto.setDate(vo.getRepresentativeStart());
        dto.setYear(vo.getRepresentativeStart().substring(0,4));
        dto.setMonth(vo.getRepresentativeStart().substring(5,7));
        dto.setDay(vo.getRepresentativeStart().substring(8,10));
        dto.setSalaryReason(vo.getSalaryReason());
        return dto;
    }

    private Object handleTerminate(EmployeeInfoEntity entity,EmployerCompanyEntity companyEntity,
                                   ExportContractPdfVo vo,JSONObject userInfo){
        ContractTerminatePdfVo dto = BeanUtilsMabach.doToDto(entity,ContractTerminatePdfVo.class);
        ContractMangerEntity mangerEntity = contractMangerService.getByIdE(vo.getRenewContractId());
        ContractMangerEntity oldEntity = contractMangerService.getByIdE(vo.getId());
        Date date = new Date();
        dto.setCompany(companyEntity.getName());
        dto.setContractNo(mangerEntity.getContractNo());
        dto.setYear(DateUtils.format(date,"yyyy"));
        dto.setMonth(DateUtils.format(date,"MM"));
        dto.setDay(DateUtils.format(date,"dd"));
        dto.setEndDay(DateUtils.format(oldEntity.getContractBeginDate(),"dd"));
        dto.setEndMonth(DateUtils.format(oldEntity.getContractBeginDate(),"MM"));
        dto.setEndYear(DateUtils.format(oldEntity.getContractBeginDate(),"yyyy"));
        dto.setHiredateEndDay(DateUtils.format(mangerEntity.getContractEndDate(),"dd"));
        dto.setHiredateEndMonth(DateUtils.format(mangerEntity.getContractEndDate(),"MM"));
        dto.setHiredateEndYear(DateUtils.format(mangerEntity.getContractEndDate(),"yyyy"));
        dto.setHireDay(DateUtils.format(mangerEntity.getContractBeginDate(),"dd"));
        dto.setHireMonth(DateUtils.format(mangerEntity.getContractBeginDate(),"MM"));
        dto.setHireYear(DateUtils.format(mangerEntity.getContractBeginDate(),"yyyy"));
        dto.setRepresentative(companyEntity.getRepresentative());
        dto.setPayDay(DateUtils.format(vo.getPayDate(),"dd"));
        dto.setPayMonth(DateUtils.format(vo.getPayDate(),"MM"));
        dto.setPayYear(DateUtils.format(vo.getPayDate(),"yyyy"));
        dto.setSalary(vo.getSalary());
        dto.setApplierName(userInfo.getString("name"));
        dto.setUnitName(jobController.orgMap.get(entity.getUnitId()));
        dto.setJobname(jobController.jobMap.get(entity.getJobId()));
        dto.setEndDate(DateUtils.format(oldEntity.getContractBeginDate(),"yyyy-MM-dd"));
        DimissionSettlementEntity dimissionSettlementEntity =  dimissionSettlementService.getByIdE(mangerEntity.getRelateId());
        if(!ObjectUtils.isEmpty(vo.getReason())){
            dto.setReason(vo.getReason());
        }else if(!ObjectUtils.isEmpty(dimissionSettlementEntity)){
            dto.setReason(dimissionSettlementEntity.getReason());
        }
        return dto;

    }


}
